<template>
  <div class="my-playlist">
    <div v-if="playlistCode == 200">
      <div class="my-hd" v-if="playlistData != {}">
        <div class="my-hd-pic">
          <img
            :src="playlistData.coverImgUrl"
            :alt="playlistData.name"
            class="my-pic"
          />
        </div>
        <div class="my-hd-main">
          <div class="my-title-box">
            <h1 class="my-title">
              <a href="javascript:;" :title="playlistData.name">
                {{ playlistData.name }}
              </a>
            </h1>
          </div>

          <div class="my-creator-box" v-if="playlistData.creator">
            <div class="my-creator-left">
              <a href="javascript:;" :title="playlistData.creator.nickname">
                <img
                  :src="playlistData.creator.avatarUrl"
                  :alt="playlistData.creator.nickname"
                  class="my-creator-pic"
                />
              </a>
            </div>
            <h4 class="my-creator">
              <a href="javascript:;" :title="playlistData.creator.nickname">{{
                playlistData.creator.nickname
              }}</a>
            </h4>
            <span class="my-createTime"
              >{{ playlistData.createTime | showDate }} 创建</span
            >
          </div>

          <div
            class="my-tags"
            v-if="playlistData.tags && playlistData.tags.length > 0"
          >
            标签：
            <span>
              <a
                href="javascript:;"
                v-for="tag in playlistData.tags"
                :key="tag"
                class="tag-item"
                @click="clickPlaylist(tag)"
              >
                {{ tag }}
              </a>
            </span>
          </div>

          <div class="my-songlist-toolbar">
            <a class="my-btn-blue">
              <span class="btn-play" @click="playAll">
                <i class="iconfont">&#xe8a8;</i>
                全部播放
              </span>
              <span class="btn-add">
                <i class="iconfont" @click="addAll">&#xe603;</i>
              </span>
            </a>
            <a class="my-btn">
              <i class="iconfont">&#xe8a6;</i>
              下载
            </a>
            <a class="my-btn">
              <i class="iconfont">&#xe8af;</i>
              分享
            </a>
            <a class="my-btn">
              <i class="iconfont">&#xe8a7;</i>
              评论
            </a>
          </div>
        </div>
        <div class="my-hd-edit" v-if="playlistData.specialType != 5">
          <i class="iconfont">&#xec53;</i>
          <span @click="clickEdit">编辑</span>
        </div>
      </div>
      <div class="my-relative-box">
        <h2 class="my-relative-hd">介绍</h2>
        <div v-if="haveDesc">
          <pre :class="['my-description', { limit: isLimit }]">{{
            playlistData.description
          }}</pre>
          <a class="my-description-height" @click="changeLimit">{{ show }}</a>
        </div>
        <div v-if="!haveDesc" class="my-not-desc">暂无介绍</div>
      </div>

      <div class="my-list">
        <div class="songlist-message">
          <h3>歌曲列表</h3>
          <span class="songlist-num">{{ songs.length }}首歌</span>
          <span class="songlist-play"
            >播放：<span class="playcount">{{ playlistData.playCount }}</span
            >次</span
          >
        </div>
        <div v-if="showSong">
          <art-song-list
            :songs="songs"
            :showArtist="true"
            :showAlbum="true"
            @delSong="delSong"
          ></art-song-list>
        </div>
        <div v-if="!showSong" class="noSong">
          <p class="hd">暂无音乐</p>
          <p class="m">
            点击
            <i class="iconfont">&#xe8c8;</i>
            即可将你喜欢的音乐收藏到“我的音乐” 马上去
            <a href="javascript:;" @click="clickHome">发现音乐</a>
          </p>
        </div>
      </div>

      <div class="my-comment" v-if="commentData && showSong">
        <comment ref="comment" :commentData="commentData"></comment>
        <div class="comment-el-pagination" v-if="commentData.total > 20">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage"
            :page-size="20"
            layout="prev, pager, next, jumper"
            :total="commentData.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="delTip" id="delTip" v-if="showDelTip">
      <div class="my-del-tip" id="mdt">
        <h4 class="my-del-hd">
          提示
          <span class="close" @click="showDelTip = false">
            <i class="iconfont">&#xec47;</i>
            关闭
          </span>
        </h4>
        <div class="my-del-bd">
          <div class="my-del-cont">
            <p>
              确定删除歌曲：<span class="my-del-song">{{ delSongName }}</span>
              ？
            </p>
          </div>
          <div class="my-del-btn">
            <el-button type="primary" size="small" @click="sureDel"
              >确定</el-button
            >
            <el-button size="small" @click="showDelTip = false">取消</el-button>
          </div>
        </div>
      </div>
    </div>
    <small-tip :tipMessage="smallTipMsg" v-if="showSmallTip"></small-tip>
  </div>
</template>

<script>
import { formatDate } from "common/date.js";
import ArtSongList from "components/songlist/ArtSongList";
import Comment from "views/toplists/child/Comment.vue";
import notLogged from "assets/images/notLogged.gif";
import SmallTip from "components/tip/SmallTip";

export default {
  inject: ["reload"],
  components: {
    ArtSongList,
    Comment,
    SmallTip,
  },
  filters: {
    // 过滤器转换时间戳
    showDate(value) {
      const date = new Date(value);
      return formatDate(date, "yyyy-MM-dd");
    },
  },

  data() {
    return {
      playlistData: {},
      commentData: {},
      songs: [],
      showSong: true,
      currentPage: 1,
      isLimit: true,
      show: "[展开]",
      id: "",
      playlistCode: 200,
      notLogged: notLogged,
      // 有介绍
      haveDesc: true,
      // 显示删除提示
      showDelTip: false,
      h: 0,
      // 要删除的歌曲id
      dsId: 0,
      delSongName: "",
      showSmallTip: false,
      smallTipMsg: "操作提示",
    };
  },
  mounted() {
    this.id = this.$route.query.id;
    // console.log(this.id);
    // this.id = "7155787551";
    this.getPlaylistDetail();
  },
  methods: {
    sureDel() {
      // console.log(this.dsId);
      this.axios({
        url: "/playlist/tracks?op=del&pid=" + this.id + "&tracks=" + this.dsId,
      })
        .then((res) => {
          // 我的音乐 里的操作
          this.smallTipMsg = "删除歌曲成功！";
          this.showSmallTip = true;
          this.showDelTip = false;
          setTimeout(() => {
            this.showSmallTip = false;
          }, 1000);
          this.$emit("updateP");
          this.getPlaylistDetail();
        })
        .catch((err) => {
          console.log(err);
          this.smallTipMsg = "删除歌曲失败！";
          this.showSmallTip = true;
          this.showDelTip = false;
          setTimeout(() => {
            this.showSmallTip = false;
          }, 1000);
          this.$emit("updateP");
          this.getPlaylistDetail();
        });
    },
    delSong(id, name) {
      this.showDelTip = true;
      this.dsId = id;
      this.delSongName = name;
      // this.setTop();
    },
    clickEdit() {
      this.playMyEdit(this.id);
    },
    clickHome() {
      this.$router.push("/home");
    },
    playAll() {
      if (this.songs.length != 0) {
        var newItem = [];
        this.songs.forEach((item) => {
          newItem[newItem.length] = {
            id: item.id,
            name: item.name,
            ar: item.ar,
            al: item.al,
            mv: item.mv,
            dt: item.dt,
            fee: item.fee,
          };
        });
        this.playSongs(newItem, true);
      } else {
        console.log("kong");
      }
    },
    addAll() {
      if (this.songs.length != 0) {
        var newItem = [];
        this.songs.forEach((item) => {
          newItem[newItem.length] = {
            id: item.id,
            name: item.name,
            ar: item.ar,
            al: item.al,
            mv: item.mv,
            dt: item.dt,
            fee: item.fee,
          };
        });
        console.log(newItem);
        this.addSongs(newItem, true);
      }
    },
    clickPlaylist(cat) {
      this.playPlaylists(cat);
    },
    clickPlaylistDetail(id) {
      this.playPlaylistDetail(id);
    },
    changeLimit() {
      this.isLimit = !this.isLimit;
      if (this.isLimit) {
        this.show = "[展开]";
        document.documentElement.scrollTo({
          top: 141,
          behavior: "smooth",
        });
      } else {
        this.show = "[收起]";
      }
    },
    handleCurrentChange() {
      // console.log(this.currentPage);
      // console.log(`当前页: ${val}`);
      this.getComment();
      // 点击分页后，滚动到评论的头部，141为页面头部组件的高度
      console.log(this.$refs.comment.$el.offsetTop);
      this.$emit("pScroll", this.$refs.comment.$el.offsetTop + 50);
    },
    getComment() {
      this.axios({
        /* 歌单评论接口地址 */
        url:
          "/comment/playlist?id=" +
          this.id +
          "&offset=" +
          (this.currentPage - 1) * 20,
      })
        .then((res) => {
          this.commentData = res.data;
          console.log(this.commentData);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getSongList() {
      this.axios({
        /* 歌曲列表接口地址 */
        url: "/playlist/track/all?id=" + this.id + "&timerstamp=" + Date.now(),
      })
        .then((res) => {
          this.songs = res.data.songs;
          // console.log(res.data.songs.length);
          console.log(this.songs);
          if (res.data.songs.length == 0) {
            this.showSong = false;
          } else {
            this.showSong = true;
          }
          // console.log(this.songs);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getPlaylistDetail() {
      this.axios({
        /* 歌单详情 */
        url: "/playlist/detail?id=" + this.id + "&timerstamp=" + Date.now(),
      })
        .then((res) => {
          // console.log(res.data);
          // if (res.data) {
          this.playlistCode = 200;
          this.playlistData = res.data.playlist;
          if (res.data.playlist.description) {
            this.haveDesc = true;
          } else {
            this.haveDesc = false;
          }
          this.getComment();
          this.getSongList();
          // console.log("200");
          // } else {
          //   this.playlistCode = 20001;
          //   // console.log("20001");
          // }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
.my-playlist {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.my-hd {
  position: relative;
  min-height: 250px;
  overflow: hidden;
}
.my-hd-pic {
  position: absolute;
  left: 25px;
  top: 25px;
  width: 200px;
  height: 200px;
}
.my-pic {
  width: 100%;
  height: 100%;
}
/* my-hd-main */
.my-hd-main {
  position: relative;
  margin-left: 250px;
  min-height: 250px;
}
.my-title,
.my-creator {
  font-weight: 400;
  white-space: normal;
  overflow-wrap: break-word;
}
.my-title {
  margin: 0;
}
.my-title-box,
.my-creator-box {
  min-height: 50px;
  line-height: 50px;
}
.my-creator-box {
  position: relative;
  margin: 15px 0 15px;
  overflow: hidden;
}
.my-creator-left {
  float: left;
  height: 50px;
  width: 50px;
}
.my-creator-left a {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 50px;
}
.my-creator-pic {
  height: 50px;
  width: 50px;
}
.my-creator {
  float: left;
  font-size: 16px;
  margin: auto 20px;
}
.my-creator a:hover {
  color: var(--color-theme);
}
.my-createTime {
  font-size: 14px;
  color: #999;
}
.my-tags {
  height: 50px;
  line-height: 50px;
}
.tag-item {
  padding-right: 10px;
}
.tag-item:hover {
  color: var(--color-theme);
}
.my-hd-edit {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 14px;
}
.my-hd-edit span {
  margin-left: 5px;
  color: var(--color-theme);
}
.my-hd-edit span:hover {
  cursor: pointer;
  text-decoration: underline;
}
/* my-songlist-toolbar */
.my-songlist-toolbar .iconfont {
  font-size: 14px;
}
.my-songlist-toolbar .my-btn,
.my-songlist-toolbar .my-btn-blue {
  border-radius: 2px;
  font-size: 14px;
  margin-right: 6px;
  padding: 0 10px;
  height: 30px;
  line-height: 30px;
  display: inline-block;
  white-space: nowrap;
  box-sizing: border-box;
  overflow: hidden;
  cursor: pointer;
}
.my-btn-blue {
  border: 1px solid var(--color-theme);
  background-color: var(--color-theme);
  color: #fff;
}
.my-btn {
  border: 1px solid #c9c9c9;
  color: #000;
}
.my-relative-hd {
  margin-bottom: 10px;
  font-weight: 400;
  height: 36px;
  line-height: 36px;
}
/* my-relative-box */
.my-relative-box {
  position: relative;
  padding-left: 10px;
  width: 100%;
  overflow: hidden;
}
.my-description {
  width: 100%;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  font-size: 14px;
  line-height: 20px;
  margin-bottom: 20px;
  white-space: pre-wrap;
  white-space: -moz-pre-wrap;
  white-space: -pre-wrap;
  white-space: -o-pre-wrap;
  /* *word-wrap: break-word; */
  /* *white-space: normal; */
}
.my-description.limit {
  max-height: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.my-description-height {
  float: right;
  font-size: 14px;
  color: var(--color-theme);
  cursor: pointer;
  margin-bottom: 20px;
}
.my-not-desc {
  margin-bottom: 20px;
  height: 28px;
  line-height: 28px;
  padding-left: 20px;
  font-size: 14px;
  color: #999;
}
/* 歌曲列表头 */
.my-list {
  width: 100%;
}
.my-list .songlist-message {
  margin-left: 10px;
  height: 50px;
  line-height: 50px;
  border-bottom: 2px solid var(--color-theme);
}
.my-list .songlist-message h3 {
  float: left;
  font-weight: 400;
  margin-right: 15px;
}
.my-list .songlist-message .songlist-num {
  font-size: 14px;
  color: #999;
}
.my-list .songlist-message .songlist-play {
  float: right;
  margin-right: 20px;
  font-size: 14px;
}
.my-list .songlist-message .songlist-play .playcount {
  color: var(--color-theme);
}
.my-list .art-songlist .songlist-songname,
.my-list .art-songlist .songlist-hd-name {
  width: 60%;
}
.my-list .art-songlist .songlist-artist,
.my-list .art-songlist .songlist-album,
.my-list .art-songlist .songlist-hd-author,
.my-list .art-songlist .songlist-hd-album {
  width: 15%;
}
.my-list .art-songlist-hd,
.my-list .art-songlist .songlist-item {
  padding-right: 0;
}
.comment-el-pagination {
  position: relative;
  text-align: center;
  margin: 20px auto;
}
.my-comment {
  padding-left: 10px;
}
.noSong {
  position: relative;
  height: 250px;
  width: 100%;
}
.noSong .hd {
  margin-top: 100px;
  height: 50px;
  line-height: 50px;
  font-weight: 700;
  font-size: 24px;
  text-align: center;
}
.noSong .m {
  height: 30px;
  line-height: 30px;
  font-size: 14px;
  color: #999;
  text-align: center;
}
.noSong .iconfont,
.noSong a {
  color: var(--color-theme);
}
/* my-del-tip */
.delTip {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1006;
}
.my-del-tip {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  height: 200px;
  width: 500px;
  background-color: var(--color-theme-light-5);
  border-radius: 10px;
  box-shadow: 0 0 10px 3px #999;
  overflow: hidden;
}
.my-del-hd {
  padding: 0 10px 0 20px;
  height: 40px;
  line-height: 40px;
  font-size: 20px;
  color: #f2f2f2;
  background-color: var(--color-theme);
  cursor: move;
}
.my-del-hd .close {
  position: absolute;
  right: 5px;
  width: 30px;
  height: 30px;
  text-align: center;
  font-size: 0;
  cursor: pointer;
}
.my-del-hd .close .iconfont {
  font-size: 16px;
}
.my-del-bd {
  position: relative;
  width: 100%;
  height: 160px;
  text-align: center;
}
.my-del-bd .my-del-cont {
  position: relative;
  height: 110px;
  line-height: 30px;
  font-size: 16px;
  color: #000;
}
.my-del-bd .my-del-cont p {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 480px;
  overflow: hidden;
  white-space: normal;
  overflow-wrap: break-word;
}
.my-del-bd .my-del-song {
  color: var(--color-theme);
}
.my-del-bd .my-del-btn {
  display: flex;
  justify-content: space-evenly;
}
</style>